<?php
class Customers extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getAllCustomer($keyword = '',$user='',$sort='',$order='',$offset='',$paging='')
    {
        $data = array();
        $sql  = "SELECT *,c.id ";
        $sql .=" FROM customers c ";
		$sql .= " WHERE c.customer_name<>'' AND c.spv_approve = 1 ";
		if($keyword){
			$sql.=	" AND c.customer_name LIKE '%".$keyword."%'";
		}
		if($user){
			if(($this->session->userdata('user_pos')== $this->config->item('ta'))) 	
                $sql.="AND  rev ='".$user."'";
			elseif(($this->session->userdata('user_pos')== $this->config->item('se')))
					 $sql.="AND rev2='".$user."'";
			elseif(($this->session->userdata('user_pos')== $this->config->item('ae')))
					 $sql.="AND rev3='".$user."'";
		}
		
		if($sort){
			$sql.=	" ORDER BY c.".$sort." ".$order;
		}
		
		if($paging){
			$sql.=	" LIMIT ";
			 if($offset){
				$sql.=	$offset.",";
			}else{
				$sql.=	"0,";
			}
			$sql.=	$paging;
		}
        $Q = $this->db->query($sql);
        if ($Q->num_rows() > 0):
            foreach ($Q->result_array() as $row):
                $data[] = $row;
            endforeach;
        endif;
		$Q->free_result();
        return $data;
	}
	
	function getApproval($limit='',$type=''){
        $sql =" SELECT * FROM customers ";
		$sql.=" WHERE customer_name<>'' ";
		
		if(($this->session->userdata('user_pos')== $this->config->item('se'))):
			$sql.=" AND se_approve = 0 AND spv_approve=0 ";
		elseif(($this->session->userdata('user_pos')== $this->config->item('spv')) || ($this->session->userdata('user_pos')== $this->config->item('mgr'))):
			$sql.=" AND se_approve = 1 AND spv_approve=0 ";
		endif;
		
		if($type){
			$sql.=" AND status_id=".$type;
		}
		
        $sql.=" ORDER BY date_stamp ASC";
        $sql.=" LIMIT ".$limit;
        $query=$this->db->query($sql);
		return $query->result_array();
	}
	
	function getCountCustomer($id='',$user=''){
        $data=array();
		$sql =	" SELECT * ";
        $sql.=	" FROM customers c WHERE c.id >=0";
        if($id){
            $sql.=	" AND c.id='".$id."'";
        }
		if($user){
		
			if(($this->session->userdata('user_pos')== $this->config->item('ta'))) 	
                $sql.=" AND  c.rev ='".$user."'";
			elseif(($this->session->userdata('user_pos')== $this->config->item('se')))
					 $sql.="AND c.rev2='".$user."'";
			elseif(($this->session->userdata('user_pos')== $this->config->item('ae')))
					 $sql.="AND c.rev3='".$user."'";
        }
        $Q	 =	$this->db->query($sql);
		return $Q->num_rows();
    }
	
	function remove($id)
    {
        $id = trim($id);
        $id = strtolower($id);
        $this->db->where('id', $id);
        $this->db->delete('customers');
    }
	
	function getSave(){
		if($this->session->userdata('user_pos')== $this->config->item('ta')):
            $rev 		=  $this->session->userdata('user_id');
			$rev2 		=  '';
			$rev3 		=  '';
			$se	  		=  0;
			$se_name	= '';
			$spv   		=  0;
			$spv_name	= '';
        elseif($this->session->userdata('user_pos')== $this->config->item('se')):
			$rev  		=  '';
			$rev2 		=  $this->session->userdata('user_id');
			$rev3 		=  '';
			$se	  		=  1 ;
			$se_name=$this->session->userdata('user_id');
			$spv   		=  0;
			$spv_name	= '';
        elseif($this->session->userdata('user_pos')== $this->config->item('ae')):
			$rev  		=  '';
			$rev2 		=  '';
			$rev3 		=  $this->session->userdata('user_id');
			$se	  		=  0;
			$se_name	=  '';
			$spv   		=  0;
			$spv_name	= '';
		else:
            $rev 		=  $this->session->userdata('user_id'); 
			$rev2 		=  '';
			$rev3 		=  '';
			$rev 		=  $this->session->userdata('user_id');
			$rev2 		=  '';
			$rev3 		=  '';
			$se	  		=  0;
			$se_name	=  '';
			$spv   		=  0;
			$spv_name	=  '';	
		endif;	
		
        $value = array  (
                            'sector_id'     =>  trim($this->input->post('sector')),
                            'rev_group'     =>  0,
                            'group_id'      =>  trim($this->input->post('group')),
                            'source_id'     =>  trim($this->input->post('source')),
                            'status_id'     =>  trim($this->input->post('status')),
                            'customer_name' =>  $this->input->post('name'),
                            'customer_from' =>  $this->input->post('from'),
                            'pic'           =>  $this->input->post('pic'),
                            'pic_pos'       =>  $this->input->post('pic_pos'),
                            'pic2'          =>  $this->input->post('pic'),
                            'pic2_pos'      =>  $this->input->post('pic_pos'),
                            'fax'           =>  $this->input->post('fax'),
                            'address'       =>  $this->input->post('address'),
                            'tlp'           =>  $this->input->post('tlp'),
                            'ext'           =>  $this->input->post('ext'),
                            'hp'           =>  $this->input->post('hp'),
                            'email'         =>  $this->input->post('email'),
                            'city'          =>  $this->input->post('city'),
                            'zip_code'      =>  $this->input->post('zipcode'),
                            'desc'          =>  $this->input->post('desc'),
                            'rev'           =>  $rev,
                            'rev2'          =>  $rev2,
                            'rev3'          =>  $rev3,
							'se_approve'	=>	$se,
							'se_name'		=>	$se_name,
							'spv_approve'	=>	$spv,
							'spv_name'		=>	$spv_name,
                            'enabled'       =>  0,
                            'user_stamp'    =>  $this->session->userdata('user_id'),
                            'date_stamp'    =>  date('Y-m-d')
                        );
        $this->db->insert('customers',$value);
	}	
	
	function getUpdateProspectTA($id){
		$value = array(
					'se_approve' => 1,
					'se_name'	 => $this->session->userdata('user_id') 	
		);
		$this->db->where('id',$id);
		$this->db->update('customers',$value);
	}
	
	function getProspectUser($id){
		$value = array(
					'se_approve' => 0,
					'se_name'	 => '' 	
		);
		$this->db->where('id',$id);
		$this->db->update('customers',$value);
	}
	
	function getUpdateProspectSE($id){
		$value = array(
					'spv_approve' => 1,
					'spv_name'	  => $this->session->userdata('user_id') 	
		);
		$this->db->where('id',$id);
		$this->db->update('customers',$value);
	}

	function getDeleteCustomer($id){
		$this->db->where('id',$id);
		$this->db->delete('customers',$value);
	}	
	
}	